package com.yunquan.ohana.ui.history.list.adapter

import android.graphics.drawable.Drawable
import android.view.View
import androidx.databinding.ViewDataBinding
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.yunquan.ohana.R
import com.yunquan.ohana.databinding.ItemHistoryBinding
import com.yunquan.ohana.ui.history.list.HistoryItemViewModel
import com.yunquan.ohana.utils.GlideApp
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter
import java.io.File

class HistoryListAdapter : BindingRecyclerViewAdapter<HistoryItemViewModel>() {

    override fun onBindBinding(binding: ViewDataBinding, variableId: Int, layoutRes: Int, position: Int, item: HistoryItemViewModel?) {
        super.onBindBinding(binding, variableId, layoutRes, position, item)
        if (binding is ItemHistoryBinding) {
            val bean = item?.data?.photos?.get(0)
            if (bean != null) {
                var path: String? = null
                var file: File? = null
                if (bean.type == 1) {
                    if (bean.localCropPath != null) {
                        file = File(bean.localCropPath)
                        if (!file.exists() && bean.localFilePath != null) {
                            file = File(bean.localFilePath)
                        }
                    } else if (bean.localFilePath != null) {
                        file = File(bean.localFilePath)
                    } else {
                        path = bean.fileUrl
                    }
                } else {
                    if (bean.thumbnail != null) {
                        file = File(bean.thumbnail!!)
                    } else {
                        path = bean.videoImgUrl ?: bean.fileUrl
                    }
                }
                val glide = if (path != null) {
                    GlideApp.with(binding.root.context).load(path)
                } else if (file?.exists() == true) {
                    GlideApp.with(binding.root.context).load(file)
                } else {
                    GlideApp.with(binding.root.context).load(R.mipmap.ic_default_image)
                }
                glide.override(200, 300)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .skipMemoryCache(false)
                    .error(R.mipmap.ic_default_image)
                    .listener(object : RequestListener<Drawable> {
                        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>, isFirstResource: Boolean): Boolean {
                            binding.picLoading.visibility = View.GONE
                            return false
                        }

                        override fun onResourceReady(
                            resource: Drawable,
                            model: Any,
                            target: Target<Drawable>?,
                            dataSource: DataSource,
                            isFirstResource: Boolean
                        ): Boolean {
                            binding.picLoading.visibility = View.GONE
                            return false
                        }
                    })
                    .into(binding.ivPic)
            } else {
                binding.ivPic.setImageResource(R.mipmap.ic_default_image)
            }
        }

    }
}